package admin

import (
	"github.com/gin-gonic/gin"
	"mojieshi/define"
	"mojieshi/models"
	"net/http"
	"strconv"
)

// 获取城市数据
func CityList(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var list []models.MProvince
	var count int64
	if jsonData.Page == 0 {
		jsonData.Page = -1
	}
	if jsonData.Limit == 0 {
		jsonData.Limit = -1
	}
	offsetVal := (jsonData.Page - 1) * jsonData.Limit
	if jsonData.Page == -1 && jsonData.Limit == -1 {
		offsetVal = -1
	}
	tx := models.DB.Model(new(models.MProvince))
	if jsonData.Status > 0 {
		tx = tx.Where("is_kai=?", jsonData.Status)
	}
	if jsonData.Aid > -1 {
		tx = tx.Where("pid=?", jsonData.Aid)
	}
	if jsonData.Name != "" {
		tx = tx.Where("fullname LIKE ?", "%"+jsonData.Name+"%")
	}
	if jsonData.Jid > 0 {
		tx = tx.Where("ji_bie=?", jsonData.Jid)
	}
	tx = tx.Count(&count).Limit(jsonData.Limit).Offset(offsetVal).Find(&list)
	if err != nil {
		define.Response.ResultFail(c, 10001, err.Error(), "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

// 城市修改
func CityAdd(c *gin.Context) {
	//获取传值
	var jsonData models.MProvince
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	//修改数据
	var editData = map[string]interface{}{
		"is_kai": jsonData.IsKai,
	}
	err = models.DB.Model(new(models.MProvince)).Where("id=?", jsonData.ID).Updates(&editData).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10003,
			"msg":  err.Error(),
		})
		return
	}
	srt := ""
	if jsonData.IsKai == 1 {
		srt = "开启了 "
	} else {
		srt = "关闭了 "
	}
	//写入日志
	userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
	userName, bid := models.UserName(userId)
	logData := "管理员：" + userName + "，" + srt + strconv.Itoa(jsonData.ID) + "的服务"
	models.LogAdd(3, "修改城市服务状态", userId, logData, bid, 1)

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

type JsonDataCity struct {
	Pid int   `json:"pid"`
	Ids []int `json:"ids"`
}

// 批量城市修改
func CityEdit(c *gin.Context) {
	//获取传值
	var jsonData JsonDataCity
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	tx := models.DB.Begin()
	//修改数据
	var Data = map[string]interface{}{
		"is_kai": 2,
	}
	err = tx.Model(new(models.MProvince)).Where("pid=?", jsonData.Pid).Updates(&Data).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10003,
			"msg":  err.Error(),
		})
		tx.Rollback()
		return
	}
	//修改数据
	var editData = map[string]interface{}{
		"is_kai": 1,
	}
	err = tx.Model(new(models.MProvince)).Where("id IN ?", jsonData.Ids).Updates(&editData).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10003,
			"msg":  err.Error(),
		})
		tx.Rollback()
		return
	}
	var result string
	for i := 0; i < len(jsonData.Ids); i++ {
		if i == 0 {
			result += strconv.Itoa(jsonData.Ids[i])
		} else {
			result += "," + strconv.Itoa(jsonData.Ids[i])
		}
	}
	//写入日志
	userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
	userName, bid := models.UserName(userId)
	logData := "管理员：" + userName + "，批量修改了id:" + result + "的状态"
	models.LogAdd(3, "修改城市服务状态", userId, logData, bid, 1)
	tx.Commit()
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

type jsonCity struct {
	Id []models.MProvince `json:"id"`
}

func AddCity(c *gin.Context) {
	//获取传值
	var jsonData jsonCity
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	var listData []models.MProvince
	listData = jsonData.Id
	err = models.DB.Model(new(models.MProvince)).Create(&listData).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10001,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}
